<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Altura Running</title>
<meta name="author" content="José Rogério" />
<meta name="copyright" content="realinf.com" />
<meta name="language" content="English" />

<style type="text/css">
	body,td,th {	font-family: Trebuchet MS, Arial, Helvetica, sans-serif;	font-size: 12px;	color: #333;}
	body {	margin-left: 0px;	margin-top: 30px;	margin-right: 0px;	margin-bottom: 0px;}
	.maindiv{ width:690px; margin:0 auto;}
	.textbox{ padding:2px 4px; width:200px;}
	.submit{ border:solid 1px #008000; background:#008000; color:#FFF; font-weight:bold;}
</style>

<link rel="stylesheet" media="screen" href="calendar.css"  />

</head>
<body>


<div style="text-align:center;">
    
<div class="maindiv">
<form method="post">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td colspan="3" align="left" valign="middle" bgcolor="#008000"><div style="margin:0px 10px; font-weight:bold; color:#FFF; font-size:16px;">Exercícios no mês</div></td>
    </tr>
  <tr>
    <td colspan="3" align="left" valign="middle"></td>
    </tr>
  <tr>
    <td colspan="3" align="left" valign="middle">
	<?php
		include_once '../include/Functions.php';
		session_start();
	//Draw Calendar	
	function draw_calendar($month,$year){

		// Draw table for Calendar 
		$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

		// Draw Calendar table headings 
		$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

		//days and weeks variable for now ... 
		$running_day = date('w',mktime(0,0,0,$month,1,$year));
		$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
		$days_in_this_week = 1;
		$day_counter = 0;
		$dates_array = array();

		//get exercise
			$person_id = $_SESSION['user_id'];
			$sql = "SELECT id, (ex_date_time/1000) AS _date 
					FROM `exercise` 
					WHERE `ex_date_time` BETWEEN UNIX_TIMESTAMP('$year-$month-01')*1000 AND UNIX_TIMESTAMP('$year-$month-$days_in_month')*1000
					AND id_person = $person_id;";
			echo $sql;
			$mat = Functions::getDataSql($sql);
			
			for($i=0; $i < 31; $i++)
				$exercises[$i]=0;
			for( $i=0; $i < count($mat); $i++ ) {
				$exercises[date('d',$mat[$i]["_date"])] = $mat[$i]["id"];
			}
		// row for week one 
		$calendar.= '<tr class="calendar-row">';

		// Display "blank" days until the first of the current week 
		for($x = 0; $x < $running_day; $x++):
			$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
			$days_in_this_week++;
		endfor;

		// Show days.... 
		for($list_day = 1; $list_day <= $days_in_month; $list_day++):
			if($list_day==date('d') && $month==date('n'))
			{
				$currentday='currentday';
			}else
			{
				$currentday='';
			}
			$calendar.= '<td class="calendar-day '.$currentday.'">';
			
				// Add in the day number
				if($list_day<date('d') && $month==date('n'))
				{
					$showtoday='<strong class="overday">'.$list_day.'</strong>';
				}else
				{
					$showtoday=$list_day;
				}
				
				if( $exercises[$list_day] != 0)
					$calendar.= '<div class="day-number">'.$showtoday."<a href='showDetail.php?exercise_id=".$exercises[$list_day]."'><></href></div>'";
				else
					$calendar.= '<div class="day-number">'.$showtoday.'</div>';

			// Draw table end
			$calendar.= '</td>';
			if($running_day == 6):
				$calendar.= '</tr>';
				if(($day_counter+1) != $days_in_month):
					$calendar.= '<tr class="calendar-row">';
				endif;
				$running_day = -1;
				$days_in_this_week = 0;
			endif;
			$days_in_this_week++; $running_day++; $day_counter++;
		endfor;

		// Finish the rest of the days in the week
		if($days_in_this_week < 8):
			for($x = 1; $x <= (8 - $days_in_this_week); $x++):
				$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
			endfor;
		endif;

		// Draw table final row
		$calendar.= '</tr>';

		// Draw table end the table 
		$calendar.= '</table>';
		
		// Finally all done, return result 
		return $calendar;
	}

	echo '<h2>'.date('M').' '.date('Y').'</h2>';
	echo draw_calendar(date('n'),date('Y'));
?>
</td>
  </tr>
  </table>
</form>
</div>

</body>
</html>